package com.gitee.feizns.quickstart.jackson.serializer;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author feizns
 * @since 2025/8/5
 */
public class DeduplicatingListSerializer extends JsonSerializer<List<?>> {

    @Override
    public void serialize(List<?> value,
                          JsonGenerator gen,
                          SerializerProvider serializers) throws IOException {
        if (value == null || value.isEmpty()) {
            gen.writeStartArray();
            gen.writeEndArray();
            return;
        }
        // 保序去重
        Collection<?> ret = new LinkedHashSet<>(value);
        serializers.defaultSerializeValue(ret, gen);
    }

}
